package com.colorlight.cloud.complete.entity.enumerate;

import com.colorlight.cloud.common.exception.ParameterException;
import com.colorlight.cloud.common.model.ExceptionEnum;
import com.colorlight.cloud.gateway.api.message.constraint.basics.ienum.EnumTrans;
import com.colorlight.iot.proto.complete.entity.CompleteEntity;
import lombok.Getter;

import java.util.Arrays;
import java.util.Objects;

@Getter
public enum ScheduleResultStruct implements EnumTrans<CompleteEntity.ScheduleResultStruct, ScheduleResultStruct> {
    /**
     * ScheduleResultStructDefault 默认属性
     * ErrorDateFormat 错误日期
     * ErrorTimeFormat 错误时间
     * TimeEmpty time 为空
     * ErrorType type 错误
     * ErrorValue value 错误
     * SetSuccess 成功
     */
    ScheduleResultStructDefault(CompleteEntity.ScheduleResultStruct.ScheduleResultStructDefault),
    ErrorDateFormat(CompleteEntity.ScheduleResultStruct.ErrorDateFormat),
    ErrorTimeFormat(CompleteEntity.ScheduleResultStruct.ErrorTimeFormat),
    TimeEmpty(CompleteEntity.ScheduleResultStruct.TimeEmpty),
    ErrorType(CompleteEntity.ScheduleResultStruct.ErrorType),
    ErrorValue(CompleteEntity.ScheduleResultStruct.ErrorValue),
    SetSuccess(CompleteEntity.ScheduleResultStruct.SetSuccess),
    ;

    private final Integer id;
    private final String name;
    private final CompleteEntity.ScheduleResultStruct scheduleResultStruct;
    private static final long serialVersionUID = 987654321987654321L;

    ScheduleResultStruct(CompleteEntity.ScheduleResultStruct scheduleResultStruct) {
        this.id = scheduleResultStruct.getNumber();
        this.name = scheduleResultStruct.name();
        this.scheduleResultStruct = scheduleResultStruct;
    }

    /**
     * @param scheduleResultStruct
     * @return 对应的类型
     */
    public static ScheduleResultStruct getScheduleResultStruct(CompleteEntity.ScheduleResultStruct scheduleResultStruct) {
        Objects.requireNonNull(scheduleResultStruct, "scheduleResultStruct can not be null.");
        return Arrays.stream(ScheduleResultStruct.values())
                .filter(e -> e.name.equals(scheduleResultStruct.name()))
                .findFirst()
                .orElseThrow(() -> new ParameterException(ExceptionEnum.NOT_EXIST_ENUM));
    }

    @Override
    public CompleteEntity.ScheduleResultStruct toProto() {
        return this.scheduleResultStruct;
    }

    @Override
    public ScheduleResultStruct formProto(CompleteEntity.ScheduleResultStruct scheduleResultStruct) {
        return getScheduleResultStruct(scheduleResultStruct);
    }
}

